﻿module poseidon.controller.property.compilerpage;

public
{
	import dwt.all;
	import poseidon.globals;
	import poseidon.controller.gui;
	import poseidon.i18n.itranslatable;
	import poseidon.util.layoutshop;
	import poseidon.util.miscutil;
	import poseidon.controller.property.ipropertypage;
}
private import poseidon.controller.dialog.generaldialog;
private import poseidon.util.waitcursor;
private import poseidon.model.project;


class CompilerPage : AbstractPage
{
	private Text txtDMDPath, txtDMCPath, txtBudTool, txtResTool, txtDebugger, txtDDocument;
	private Button btnUseOutputWrap, btnUseThreadBuild;
	private Shell shell;

	this( Composite parent, IPropertyPage parentPage, void delegate(bool) dirtyListener )
	{
		super( parent, parentPage, dirtyListener );
		shell = parent.getShell();
		initGUI();
	}

	public void applyChanges()
	{
		Globals.DMDPath 	= std.string.strip( txtDMDPath.getText() );
		Globals.DMCPath 	= std.string.strip( txtDMCPath.getText() );
		Globals.BudExe		= std.string.strip( txtBudTool.getText() );
		Globals.RESToolExe	 = std.string.strip( txtResTool.getText() );
		Globals.DebuggerExe	 = std.string.strip( txtDebugger.getText() );
		Globals.DDcoumentDir = std.string.strip( txtDDocument.getText() );
		

		if( btnUseOutputWrap.getSelection() ) Globals.outputWRAP = DWT.WRAP; else Globals.outputWRAP = 0;
		if( btnUseThreadBuild.getSelection() ) Globals.backBuild = 1; else Globals.backBuild = 0;

		setDirty( false );
	}

	public char[] getTitle()
	{
		return Globals.getTranslation( "pref.compiler" );
	}

	private void initGUI()
	{
		setLayout( LayoutShop.createGridLayout( 3 ) );

		// DMD's path
		with( new Label( this, DWT.NONE ) )
		{
			setText( Globals.getTranslation("cp.dmd_path") );
		}

		with( txtDMDPath = new Text( this, DWT.BORDER ) )
		{
			setLayoutData( new GridData( GridData.FILL, GridData.BEGINNING, true, false, 1, 1 ));
			txtDMDPath.setText( Globals.DMDPath );
			handleEvent( null, DWT.Modify, &onAction );
		}

		// DMD's path button
		with( new Button( this, DWT.PUSH ) )
		{
			setText( "..." );
			handleEvent( null, DWT.Selection, &onBrowseDirDMD );
		}

		// DMC's path
		with( new Label( this, DWT.NONE ) )
		{
			setText( Globals.getTranslation("cp.dmc_path") );
		}

		with( txtDMCPath = new Text( this, DWT.BORDER ) )
		{
			setLayoutData( new GridData( GridData.FILL, GridData.BEGINNING, true, false, 1, 1 ) );
			txtDMCPath.setText( Globals.DMCPath );
			handleEvent( null, DWT.Modify, &onAction );
		}

		// DMC's path button
		with( new Button( this, DWT.PUSH ) )
		{
			setText( "..." );
			handleEvent( null, DWT.Selection, &onBrowseDirDMC );
		}

		// Bud Tool
		with( new Label( this, DWT.NONE ) )
		{
			setText( Globals.getTranslation("cp.build_path") );
		}

		with( txtBudTool = new Text( this, DWT.BORDER ) )
		{
			setLayoutData( new GridData( GridData.FILL, GridData.BEGINNING, true, false, 1, 1 ) );
			txtBudTool.setText( Globals.BudExe );
			handleEvent( null, DWT.Modify, &onAction );
		}

		// Bud tool button
		with( new Button( this, DWT.PUSH ) )
		{
			setText( "..." );
			handleEvent( null, DWT.Selection, &onBrowseBud );
		}
		

		// Resource Editor
		with( new Label( this, DWT.NONE ) )
		{
			setText( Globals.getTranslation("cp.res_path") );
		}

		with( txtResTool = new Text( this, DWT.BORDER ) )
		{
			setLayoutData( new GridData( GridData.FILL, GridData.BEGINNING, true, false, 1, 1 ) );
			txtResTool.setText( Globals.RESToolExe );
			handleEvent( null, DWT.Modify, &onAction );
		}

		// Resource Editor tool button
		with( new Button( this, DWT.PUSH ) ) 
		{
			setText( "..." );
			handleEvent( null, DWT.Selection, &onBrowseFileRes );
		}


		// Debugger
		with( new Label( this, DWT.NONE ) )
		{
			setText( Globals.getTranslation("cp.ddbg_path") );
		}

		with( txtDebugger = new Text( this, DWT.BORDER ) )
		{
			setLayoutData( new GridData( GridData.FILL, GridData.BEGINNING, true, false, 1, 1 ) );
			setText( Globals.DebuggerExe );
			handleEvent( null, DWT.Modify, &onAction );
		}

		// Debugger button
		with( new Button( this, DWT.PUSH ) ) 
		{
			setText( "..." );
			handleEvent( null, DWT.Selection, &onBrowseDebugger );
		}

		// horizontal line
		with( new Label( this, DWT.SEPARATOR | DWT.HORIZONTAL ) )
			setLayoutData( new GridData( GridData.FILL, GridData.CENTER, true, false, 3, 1 ) );


		// D Document
		with( new Label( this, DWT.NONE ) )
		{
			//setText( Globals.getTranslation("cp.ddbg_path") );
			setText( "D Document" );
		}

		with( txtDDocument = new Text( this, DWT.BORDER ) )
		{
			setLayoutData( new GridData( GridData.FILL, GridData.BEGINNING, true, false, 1, 1 ) );
			setText( Globals.DDcoumentDir );
			handleEvent( null, DWT.Modify, &onAction );
		}

		// D Dcoument button
		with( new Button( this, DWT.PUSH ) ) 
		{
			setText( "..." );
			handleEvent( null, DWT.Selection, &onBrowseDDocument );
		}

		// horizontal line
		with( new Label( this, DWT.SEPARATOR | DWT.HORIZONTAL ) )
			setLayoutData( new GridData( GridData.FILL, GridData.CENTER, true, false, 3, 1 ) );

		
		with( btnUseOutputWrap = new Button( this, DWT.CHECK ) )
		{
			setData( LANG_ID, "cp.wrap" );
			setSelection( Globals.outputWRAP );
			setLayoutData(LayoutDataShop.createGridData( GridData.GRAB_HORIZONTAL, 3 ) );
			handleEvent(null, DWT.Selection, &onAction);
		}

		with( btnUseThreadBuild = new Button( this, DWT.CHECK ) )
		{
			//setData( LANG_ID, "cp.wrap" );
			setText( "Compile / Build At Background-Thread" );
			setSelection( Globals.backBuild );
			setLayoutData(LayoutDataShop.createGridData( GridData.GRAB_HORIZONTAL, 3 ) );
			handleEvent(null, DWT.Selection, &onAction);
		}
	}

	public void restoreDefaults()
	{
		txtDMDPath.setText( "" );
		txtDMCPath.setText( "" );
		txtResTool.setText( "" );
		txtDebugger.setText( "" );
		txtDDocument.setText( "" );
		btnUseOutputWrap.setSelection( false );
		btnUseThreadBuild.setSelection( false );
		
		setDirty(true);
	}

	
	private void onAction( Event e )
	{
		setDirty(true);
	}

	private char[] browseDir()
	{
		scope wc = new WaitCursor(shell);
		
		scope dlg = new DirectoryDialog( shell, DWT.OPEN );
		dlg.setFilterPath(Globals.recentDir);
		return dlg.open();
	}		

	private char[] browseFile( char[] fileExt = "*.exe" )
	{
		scope wc = new WaitCursor(shell);

		scope dlg = new FileDialog( shell, DWT.OPEN );

		char[][] FileExtensions;
		FileExtensions ~= fileExt;

		dlg.setFilterPath( Globals.recentDir );
		dlg.setFilterExtensions( FileExtensions );		
		return dlg.open();
	}


	private void onBrowseDirDMD( Event e )
	{
		char[] fullpath = browseDir();
		if ( fullpath )	txtDMDPath.setText( fullpath );
	}

	private void onBrowseDirDMC( Event e )
	{
		char[] fullpath = browseDir();
		if ( fullpath )	txtDMCPath.setText( fullpath );
	}

	private void onBrowseBud( Event e )
	{
		char[] fullpath = browseFile();
		if ( fullpath )	txtBudTool.setText( fullpath );
	}
	
	private void onBrowseFileRes( Event e )
	{
		char[] fullpath = browseFile();
		if ( fullpath )	txtResTool.setText( fullpath );
	}

	private void onBrowseDebugger( Event e )
	{
		char[] fullpath = browseFile();
		if ( fullpath )	txtDebugger.setText( fullpath );
	}

	private void onBrowseDDocument( Event e )
	{
		char[] fullpath = browseFile( "*.*" );
		if ( fullpath )	txtDDocument.setText( fullpath );
	}		
	
}
